package com.lczyfz.istep.modules.openstack.api.nova.impl;

import com.lczyfz.istep.modules.openstack.api.nova.impl.parseJson.NovaParse;
import com.lczyfz.istep.modules.openstack.api.nova.interfaces.Flavors;
import com.lczyfz.istep.modules.openstack.api.nova.model.Flavor;
import com.lczyfz.istep.modules.openstack.constvary.ConstParams;
import com.lczyfz.istep.modules.openstack.constvary.RequestType;
import com.lczyfz.istep.modules.openstack.utils.HTTP;
import com.lczyfz.istep.modules.openstack.utils.Request;
import com.lczyfz.istep.modules.openstack.utils.RequestFactory;
import com.lczyfz.istep.modules.openstack.utils.Response;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Flavors�ӿڵ�ʵ����
 * @author jm
 */
public class FlavorsImpl implements Flavors {

	public RequestFactory factory = new RequestFactory("nova");
	
	@Override
	public List<Flavor> getFlavor() {
		List<Flavor> flavor = null;
		Request request = factory.getRequest();
		request.setUrl("/flavors");
		request.setRequestType(RequestType.GET);
		Response results = HTTP.getConnectionResult(request);

		if(ConstParams.RESPONSE_CODE_NORMAL != results.getResponseCode()){
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		JSONObject body = results.getResponseBody();
		flavor = NovaParse.resolveFlavors(body);
		return flavor;
	}

	@Override
	public List<Flavor> getFlavorDetail() {
		List<Flavor> flavor = new ArrayList<Flavor>();
		
		Request request = factory.getRequest();
		request.setUrl("/flavors/detail" );
		request.setRequestType(RequestType.GET);
		
		Response results = HTTP.getConnectionResult(request);
		if(ConstParams.RESPONSE_CODE_NORMAL != results.getResponseCode()){
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		JSONObject body = results.getResponseBody();
		flavor = NovaParse.resolveFlavorsDetail(body);
		return flavor;
	}

	@Override
	public Flavor createFlavor(Flavor flavor) {
		
		Flavor flavors = new Flavor();		
		JSONObject params = NovaParse.packageFlavor(flavor);

		Request request = factory.getRequest();
		request.setUrl("/flavors");
		request.setRequestType(RequestType.POST);
		request.setBody(params);
		Response results = HTTP.getConnectionResult(request);
		
		if( ConstParams.RESPONSE_CODE_NORMAL!= results.getResponseCode()){
			throw new RuntimeException("Failed : error code : " + results.getResponseCode());
		}
		JSONObject res = results.getResponseBody();
		flavors = NovaParse.resolveCreateFlavor(res);
		return flavors;
	}

	@Override
	public void deleteFlavor(String flavorId) {
		
		Request request = factory.getRequest();
		request.setUrl("/flavors/" + flavorId);
		request.setRequestType(RequestType.DELETE);
		
		Response results = HTTP.getConnectionResult(request);
		if(ConstParams.RESPONSE_CODE_DELETE != results.getResponseCode() )
		{
			throw new RuntimeException("Failed : HTTP error code : " + results.getResponseCode());
		}
		return;
	}
	
	
	public static void main(String[] args){

		FlavorsImpl flavordemo = new FlavorsImpl();
		Flavor flavortt = new Flavor();
//		flavortt.setFlavorId("12");
		flavortt.setFlavorName("test1");
		flavortt.setFlavorRam("512");
		flavortt.setFlavorVcpus("1");
		flavortt.setFlavorDisk("1");
		flavordemo.createFlavor(flavortt);
//		flavordemo.deleteFlavor("10");
		List<Flavor> flavors = flavordemo.getFlavor();
	//	Flavor flavorsdetail = flavordemo.getFlavorDetail("10");
		
		System.out.println("����ģ���б�����");
		for(Flavor flavor : flavors){
			System.out.println(flavor.getFlavorId()+" "+flavor.getFlavorName());
		}
		
//		System.out.println("����ģ����ϸ����");
//		List<Flavor> flavorsdetail=flavordemo.getFlavorDetail();
//		for(Flavor flavor : flavorsdetail){
//			System.out.println(flavor.getFlavorId()+" "+flavor.getFlavorName()+" "+flavor.getFlavorRam()+" "+flavor.getFlavorVcpus()+" "+flavor.getFlavorDisk());
//		}
		
		
	}
}
